package com.zy.io.file;


import cn.hutool.core.io.FileUtil;
import org.testng.annotations.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.SortedMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *  文件操作
 * @author zhangyu
 * @date 2022/3/26
*/
public class FileExample {

    @Test
    void test1(){
        System.out.println(System.getProperty("user.dir"));
        System.out.println(System.getProperty("line.separator"));
        System.out.println(System.getProperty("user.dir"));
    }

    @Test
    void test2() throws IOException {
        PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(new FileInputStream("file/demo.txt")));
        int var;
        while ((var= pushbackInputStream.read())!=-1){
            System.out.println((char) var);
        }
    }

    @Test
    void test3()throws IOException {
        FileReader fileReader = new FileReader("file/demo.txt");
        int var;
        while ((var= fileReader.read())!=-1){
            System.out.println((char) var);
        }
    }

    @Test
    void test4()throws IOException {
        SortedMap<String, Charset> stringCharsetSortedMap = Charset.availableCharsets();
        for (Map.Entry<String, Charset> stringCharsetEntry : stringCharsetSortedMap.entrySet()) {
            System.out.println(stringCharsetEntry.getKey()+"===>"+stringCharsetEntry.getValue());
        }
    }

    @Test
    void test5()  {
        try {
            File file = new File("file/words.txt"); // 指定文件路径

            RandomAccessFile raf = new RandomAccessFile(file,"rw");
            String str1 = "晴天，阴天，多云，小雨，大风，中雨，小雪，雷阵雨";    // 要写入的字符串
            String str2 = new String(str1.getBytes("GBK"),"ISO-8859-1");    // 编码转换
            raf.writeBytes(str2);    //写入文件
        } catch (IOException e) {
            System.out.print(e);
        }
    }


    @Test
    void test6()  {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("file/test.zip");
            ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
            ZipEntry zipEntry = new ZipEntry("a.txt");
            zipOutputStream.putNextEntry(zipEntry);
            zipOutputStream.closeEntry();
        } catch (IOException e) {
            System.out.print(e);
        }
    }

    @Test
    void test7()  {
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("file/employee.txt"));
            Employee employee1 = new Employee("张三", 1);
            outputStream.writeObject(employee1);
            outputStream.flush();
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("file/employee.txt"));
            Employee employee = (Employee) objectInputStream.readObject();
            System.out.println(employee);



        } catch (IOException e) {
            System.out.print(e);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }







}
